local ClientOHOH = _G.ClientOHOH;

local M = ClientOHOH.class.new(script, ClientOHOH.files.BaseObject())
local CallBackManager = ClientOHOH.load.CallBackManager();
local ConnectClass=_G.CommonOHOH.load.Connect();
local UIManager = ClientOHOH.load.UIManager();
local LanguageHelper= _G.LanguageHelper
local CarTool=_G.CommonOHOH.load.CarTool();
local Tool=require(script.Parent.tool)
ClientOHOH.load.PartCell()

local instStr={
   -- root="ctrl.List.root",--List根节点
   -- layout ="ctrl.List.layout",--表格控制器
   list = "ctrl.list",
   box = "ctrl.box",
   layout = "ctrl.Scale.Size",

   pop = "ctrl.pop.Frame",
   perIn = "ctrl.pop.in",
   name = "ctrl.pop.name",
}

function M.new(...)
	return M(...);
end;

function M.ctor(self,ctrl)
   M.super.ctor(self);
   self.ctrl=ctrl
   self.PartList={}
   self.ui=ctrl:InitUiInst2(ctrl.main_inst,instStr)

   self.data = nil--需要展示的Part的Data
   self.equipData = nil
   self.showNavType = 0
   self.showPullType = 0
   self.showKeyWords = nil
   self.popInfoCell = nil

   self.ctrl.NavChangeEvent:Connect(function(showType)
      self.ctrl.SearchCancelEvent:Action()
      self.ctrl.CancelShowInfolEvent:Action()
      --UIManager.ui_list.PartInfoPop:Hide()


      self.showNavType = showType
      self:UpdateCell(self.data)
      self:UpdateEquip(self.equipData)
   end)
   self.ctrl.PullDownChangeEvent:Connect(function(showType)
      --UIManager.ui_list.PartInfoPop:Hide()

      self.showPullType = showType
      self:UpdateCell(self.data)
      self:UpdateEquip(self.equipData)
   end)
   self.ctrl.SearchTxtEvent:Connect(function(txt)
      --UIManager.ui_list.PartInfoPop:Hide()

      self.ctrl.CancelShowInfolEvent:Action()
      self.showKeyWords = txt
      self:UpdateCell(self.data)
      self:UpdateEquip(self.equipData)
   end)
   self.ctrl.CancelShowInfolEvent:Connect(function()
      if  self.popInfoCell then
         self.popInfoCell:HideTip()
      end
   end)
   self:Big()
end;

function M.dtor(self)
	M.super.dtor(self);
end;



function M.GetGuid(self,petId,index)
   end

function M.CreateCell(self,carId,hadnum)
   local cell=  require(script.Parent.Cell).new(carId,self.ui.list,self.ctrl:GetDataBase(),nil,hadnum)
   cell:SetTitleVisibility(false)
   cell:SetPartWeightVisibility(true)
   cell:SetPartInVisibility(true)
   cell:SetPartNum(hadnum)
   cell:SetPartNumVisibility(true)


   cell.ClickEvent:Connect(function()
         self.ctrl:CellClick(cell)
  end)
    return cell
end
function M.Change(self,cell)

	if self.SelectedCell == nil then
		self.SelectedCell = cell
      cell:ShowTip()
      self.popInfoCell =cell
		cell:SetSelectedStatus(true)	
      self.ctrl:CellClickDown(cell)
		return
	end

	if self.SelectedCell == cell then
		self.SelectedCell = nil
      cell:HideTip()
      self.popInfoCell = nil
		cell:SetSelectedStatus(false)
      self.ctrl:CellCancelSelect()
	else
		self.SelectedCell:SetSelectedStatus(false)
      cell:ShowTip()
      self.popInfoCell =cell
		cell:SetSelectedStatus(true)
      self.ctrl:CellClickDown(cell)
		self.SelectedCell = cell
	end

end
---------------------------- 公共函数----------------------------------------------
function M.UpdateCell(self,data)
   self.data =data
   
   local fliterData = {}
   local searchData = {}
   
   for id,cell in pairs(self.PartList) do
      if not data[id] then --不存在
          cell:dtor() --销毁
          self.PartList[id]=nil
      end
  end
   if self.showKeyWords then
      
      for carId,count in pairs(data) do
         local carTxt =LanguageHelper:GetText(CarTool.GetPartAllInfo(carId).cfg.name)
         local compareTxt =string.lower(carTxt)
         -- if  self.showPullType == carGrade then
         --    fliterData[carId] = count
         -- end
         if string.match(compareTxt, self.showKeyWords) then
            searchData[carId] = count
         end

      end
   else
      searchData = data
   end
      
   --对store数据过滤
      --部件类型显示全部，过滤品阶
      if self.showNavType == 0  then
         if self.showPullType == 0 then
            fliterData = searchData
         else
            for carId,count in pairs(searchData) do
                  local carGrade = CarTool.GetPartAllInfo(carId).cfg.gradeId
                  if  self.showPullType == carGrade then
                     fliterData[carId] = count
                  end
            end
         end
      --显示所有品阶，过滤部件类型
      elseif  self.showPullType == 0 then
            if self.showNavType == 0  then
               fliterData = searchData
            else
               for carId,count in pairs(searchData) do
                  local carType = CarTool.GetPartAllInfo(carId).cfg.type
                  local carGrade = CarTool.GetPartAllInfo(carId).cfg.gradeId
                  if  self.showNavType  == carType then
                     fliterData[carId] = count
                  end
               end
            end
      else
      --过滤品阶，过滤部件类型
         for carId,count in pairs(searchData) do
            local carType = CarTool.GetPartAllInfo(carId).cfg.type
            local carGrade = CarTool.GetPartAllInfo(carId).cfg.gradeId
            if self.showNavType == carType and   self.showPullType == carGrade then
               fliterData[carId] = count
            end
         end
      end

   -- 创建一个新的表来存储过滤后的数据
   local HideTable = {}

   -- 遍历 table1，将不在 table2 中的数据存储到 filteredTable 中
   for key, value in pairs(data) do
      if not fliterData[key] then
         HideTable[key] = value
      end
   end

   for carId,count in pairs(self.data) do
      if carId and count then
         if self.PartList[carId] then
         --存在cell
            if  self.PartList[carId]:GetCarCount() == count then
            --不改变
            else
            --改变数值
               self.PartList[carId]:SetPartNum(count)
            end
            
         else
         --不存在cell
            
            self.PartList[carId]=self:CreateCell(carId, count)
            
         end

         self.PartList[carId]:SetShowStatus(true)
      
      end
   end
   for carId,count in pairs(HideTable) do
      if carId and count then
         if self.PartList[carId] then
            self.PartList[carId]:SetShowStatus(false)
         end
      end
   end

end


function M.UpdateEquip(self,data)
   if self.equipData then
      local isSame = Tool.IsEquipSame(self.equipData,data)
      if isSame then
         return
      end 
   end
   for carId,cell in pairs(self.PartList) do
      cell:SetHadStatus(false)
   end  
   self.equipData = data
   for _,equipId in pairs(self.equipData) do
      if equipId then
      local cellE=self.PartList[equipId]
      if cellE then
         cellE:SetHadStatus(true)
      end
      end
   end
   
   -- local HideTable = {}

   -- -- 遍历 table1，将不在 table2 中的数据存储到 HideTable 中
   -- for key, value in pairs(self.data) do
   --    if not data[key] then
   --       HideTable[key] = value
   --    end
   -- end
   -- for _ , carId in pairs(self.equipData) do
   --    if carId  then
   --       self.PartList[carId]:SetHadStatus(true)
   --    end
   -- end
   -- for _,carId in pairs(HideTable) do
   --    if carId  then
   --       if self.PartList[carId] then
   --          self.PartList[carId]:SetHadStatus(false)
   --       end
   --    end
   -- end

   -- for   _,equipId in pairs(self.Equipdata) do
   --    local cellE=self.PartList[equipId]
   --    cellE.UpdateEvent:Action()
   --    cellE:SetHadStatus(true)
   --     print("###____ UpdateEquip equipId",cellE.insts.hadIcon.Visible)
   -- end



   -- for hadId,cell in pairs(self.PartList) do
   --    --cell:UnEquip()
   -- end  

   -- for   _,equipId in pairs(data) do
   --    print("equipId",equipId)
   --    local cell=self.PartList[equipId]
   --     print("###____ UpdateEquip equipId",equipId,cell,cell.main_inst,cell.insts.hadIcon.Visible)
   --    -- assert(cell,"不存在carId:"..equipId)
   --     cell.UpdateEvent:Action()
   --       cell:SetHadStatus(true)
   --     print("###____ UpdateEquip equipId",cell.insts.hadIcon.Visible)
   --    -- --cell:Equip()
   --    -- print("###_____ afterEquip equipId",equipId,cell.insts.hadIcon.Visible)
   -- end
end

function M.GetEquipCount(self)
end

function M.GetSelectCell(self)
   return self.SelectedCell
end


--列表变大
function M.Big(self)
   self.ui.layout.CellSize=self.ui.layout:GetAttribute("big")

end

--列表变小
function M.Small(self)
  
   self.ui.layout.CellSize=self.ui.layout:GetAttribute("small")
end

return M